<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="用户" prop="user.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择用户"
            labelName = 'nikeName'
            labelValue = 'id'
            :value = "inputForm.user.id"
            :limit="1"
            @getValue='(value) => {inputForm.user.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'phone',
              label: '手机号'
            },
            {
              prop: 'nikeName',
              label: '昵称'
            }
            ]"
            :searchs="[
            {
              prop: 'phone',
              label: '手机号'
            }
            ]"
            dataListUrl="/userinfo/tUserInfo/list"
            entityBeanName="tUserInfo"
            queryEntityUrl="/userinfo/tUserInfo/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="门店" prop="shop.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择门店"
            labelName = 'shopName'
            labelValue = 'id'
            :value = "inputForm.shop.id"
            :limit="1"
            @getValue='(value) => {inputForm.shop.id=value}'
            :columns="[
             
            {
              prop: 'shopName',
              label: '门店名称'
            },
            {
              prop: 'shopAddress',
              label: '办公地址'
            },
            {
              prop: 'shopPhone',
              label: '门店联系电话'
            },
            {
              prop: 'contacts',
              label: '联系人'
            },
            {
              prop: 'contactsPhone',
              label: '联系人电话'
            }
            ]"
            :searchs="[
            {
              prop: 'shopName',
              label: '门店名称'
            },
            {
              prop: 'shopPhone',
              label: '门店联系电话'
            }
            ]"
            dataListUrl="/tshopinfo/tShopInfo/list"
            entityBeanName="tShopInfo"
            queryEntityUrl="/tshopinfo/tShopInfo/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="姓名" prop="userName"
                :rules="[
                 ]">
              <el-input v-model="inputForm.userName" placeholder="请填写姓名"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="联系方式" prop="phone"
                :rules="[
                 ]">
              <el-input v-model="inputForm.phone" placeholder="请填写联系方式"     ></el-input>
           </el-form-item>
        </el-col>
        <!-- <el-col :span="24">
            <el-form-item label="头像" prop="image"
                :rules="[
                 ]">
              <el-upload ref="image"
              v-if="visible"
              list-type="picture-card"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/tagentinfo/tAgentInfo`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {
                        $alert(`<img style='width:100%' src=' ${(file.response && file.response.url) || file.url}'/>`,  {
                          dangerouslyUseHTMLString: true,
                          showConfirmButton: false,
                          closeOnClickModal: true,
                          customClass: 'showPic'
                        });
                    }"
                    :on-success="(response, file, fileList) => {
                       inputForm.image = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.image = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="1"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="imageArra">
                    <i class="el-icon-plus"></i>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="名片" prop="businessCard"
                :rules="[
                 ]">
              <el-upload ref="businessCard"
              v-if="visible"
              list-type="picture-card"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/tagentinfo/tAgentInfo`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {
                        $alert(`<img style='width:100%' src=' ${(file.response && file.response.url) || file.url}'/>`,  {
                          dangerouslyUseHTMLString: true,
                          showConfirmButton: false,
                          closeOnClickModal: true,
                          customClass: 'showPic'
                        });
                    }"
                    :on-success="(response, file, fileList) => {
                       inputForm.businessCard = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.businessCard = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="1"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="businessCardArra">
                    <i class="el-icon-plus"></i>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="营业执照" prop="businessLicense"
                :rules="[
                 ]">
              <el-upload ref="businessLicense"
              v-if="visible"
              list-type="picture-card"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/tagentinfo/tAgentInfo`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {
                        $alert(`<img style='width:100%' src=' ${(file.response && file.response.url) || file.url}'/>`,  {
                          dangerouslyUseHTMLString: true,
                          showConfirmButton: false,
                          closeOnClickModal: true,
                          customClass: 'showPic'
                        });
                    }"
                    :on-success="(response, file, fileList) => {
                       inputForm.businessLicense = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.businessLicense = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="1"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="businessLicenseArra">
                    <i class="el-icon-plus"></i>
                  </el-upload>
           </el-form-item>
        </el-col> -->
        <el-col :span="24">
            <el-form-item label="身份证信息页" prop="idCardUp"
                :rules="[
                 ]">
              <el-upload ref="idCardUp"
              v-if="visible"
              list-type="picture-card"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/tagentinfo/tAgentInfo`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {
                      showImg(file.url);
                    }"
                    :on-success="(response, file, fileList) => {
                       inputForm.idCardUp = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.idCardUp = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="1"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="idCardUpArra">
                    <i class="el-icon-plus"></i>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="身份证国徽面" prop="idCardDown"
                :rules="[
                 ]">
              <el-upload ref="idCardDown"
              v-if="visible"
              list-type="picture-card"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/tagentinfo/tAgentInfo`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {
                      showImg(file.url);
                    }"
                    :on-success="(response, file, fileList) => {
                       inputForm.idCardDown = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.idCardDown = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="1"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="idCardDownArra">
                    <i class="el-icon-plus"></i>
                  </el-upload>
           </el-form-item>
        </el-col>
        
        <el-col :span="12" v-if="inputForm.status==2">
            <el-form-item label="拒绝原因" prop="jjPerson"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.jjPerson" placeholder="请填写拒绝原因"     ></el-input>
           </el-form-item>
        </el-col>
        
        <el-col :span="24">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button v-if="inputForm.status == 2" @click="audit(inputForm.id,1)">通过</el-button>
      <el-button v-if="inputForm.status == 2"  @click="jj(inputForm.id,3)">拒绝</el-button>
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        imageArra: [],
        businessCardArra: [],
        businessLicenseArra: [],
        idCardUpArra: [],
        idCardDownArra: [],
        inputForm: {
          id: '',
          user: {
            id: ''
          },
          shop: {
            id: ''
          },
          userName: '',
          phone: '',
          image: '',
          businessCard: '',
          businessLicense: '',
          idCardUp: '',
          idCardDown: '',
          status: '',
          jjPerson: '',
          dutyStatus: '',
          remarks: ''
        }
      }
    },
    components: {
      GridSelect
    },
    methods: {
      showImg(url) {
      this.$hevueImgPreview(url)
    },
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建经纪人`
        } else if (method === 'edit') {
          this.title = '修改经纪人'
        } else if (method === 'view') {
          this.title = '查看经纪人'
        }
        this.imageArra = []
        this.businessCardArra = []
        this.businessLicenseArra = []
        this.idCardUpArra = []
        this.idCardDownArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/tagentinfo/tAgentInfo/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.tAgentInfo)
              this.inputForm.image.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.imageArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.inputForm.businessCard.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.businessCardArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.inputForm.businessLicense.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.businessLicenseArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.inputForm.idCardUp.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.idCardUpArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.inputForm.idCardDown.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.idCardDownArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/tagentinfo/tAgentInfo/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      },
      audit(id,status) { 
        this.$confirm(`确定当前操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/tagentinfo/tAgentInfo/audit',
            method: 'post',
            params: {'id': id,"status":status}
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.$parent.refreshList()
            }
            this.loading = false
          })
        })
      },
      jj(id,status){
        this.$prompt('请输入拒绝原因', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(({ value }) => {
          if (!value){
              this.$message.error("请输入拒绝原因");
              return false
            }
          this.loading = true
          this.$http({
            url: '/tagentinfo/tAgentInfo/audit',
            method: 'post',
            params: {'id': id,"status":status,'jjPerson':value}
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.$parent.refreshList()
            }
            this.loading = false
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });       
        });
      },
    }
  }
</script>